package com.example.sharesinfo.fragment.search;


import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sharesinfo.R;
import com.example.sharesinfo.adapter.CommonAdapter;
import com.example.sharesinfo.adapter.CommonViewHolder;
import com.example.sharesinfo.base.BaseFragment;
import com.example.sharesinfo.manager.UserManager;
import com.example.sharesinfo.model.RecommendModel;
import com.example.sharesinfo.model.SearchUsersModel;
import com.example.sharesinfo.utils.LogUtils;
import com.example.sharesinfo.utils.OkHttpUtils;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class SearchUserFragment extends BaseFragment {

    private static final int GetStartSearchUsersSuccess = 1001;
    private static final int GetStartSearchUsersFailed = 1002;
    private RecyclerView mvRecordView;
    public CommonAdapter<SearchUsersModel> mAdapter;
    public List<SearchUsersModel> mList = new ArrayList<>();
    public TextView title;


    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case GetStartSearchUsersSuccess:
                    JSONArray array = (JSONArray) msg.obj;
                    for (int i = 0; i < array.length(); i++) {
                        try {
                            JSONObject object = array.getJSONObject(i);
                            SearchUsersModel model = new SearchUsersModel();
                            model.setId(Integer.parseInt(object.getString("Id")));
                            model.setImageUrl(object.getString("ImageUrl"));
                            model.setUsername(object.getString("Username"));
                            model.setFansNumber(object.getString("FansNumber"));
                            mList.add(model);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    mAdapter.notifyDataSetChanged();
                    break;
                case GetStartSearchUsersFailed:
                    Toast.makeText(getContext(), "获取数据失败", Toast.LENGTH_SHORT).show();
                    break;
            }
            return false;
        }
    });

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_user, null);
        initView(view);
        getStartSearchUsers();
        return view;
    }

    private void initView(View view) {
        title = view.findViewById(R.id.title);
        mvRecordView = view.findViewById(R.id.mRecommendView);
        mvRecordView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mvRecordView.addItemDecoration(new DividerItemDecoration(getActivity(),
                DividerItemDecoration.VERTICAL));
        mAdapter = new CommonAdapter<>(mList, new CommonAdapter.OnBindDataListener<SearchUsersModel>() {
            @Override
            public void onBindViewHolder(final SearchUsersModel model, CommonViewHolder viewHolder, int type, int position) {
                viewHolder.setImageUrl(getContext(), R.id.ci_photo, model.getImageUrl());
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UserManager.getInstance().queryUserInfo(model.getId(), getContext());
                    }
                });
                viewHolder.setText(R.id.tv_username, model.getUsername());
                viewHolder.setText(R.id.tv_fans_number, model.getFansNumber());
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.layout_hot_user;
            }
        });
        mvRecordView.setAdapter(mAdapter);

    }

    /**
     * 搜索用户初始化界面数据（无参数）
     */

    private void getStartSearchUsers() {
        String url = "http://119.23.221.142/api/user/GetStartSearchUsers";
        OkHttpUtils.getInstance().Get(url, new OkHttpUtils.okHttpListener() {

            @Override
            public void OnSuccess(String s, int code) throws JSONException {
                Message message = mHandler.obtainMessage();
                JSONObject object = new JSONObject(s);
                if (object.getString("code").equals("200")) {
                    message.what = GetStartSearchUsersSuccess;
                    message.obj = object.getJSONArray("data");
                    mHandler.sendMessage(message);
                } else {
                    message.what = GetStartSearchUsersFailed;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void OnFailed(String s) {

            }
        });
    }
}
